﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Net;
using System.Text;
using Kay.BaseLibrary.Models;
using Kay.BaseLibrary.Net;

namespace Kay.BaseLibrary.Helpers
{
    /// <summary>
    /// 网络帮助器
    /// </summary>
    public class NetworkHelper
    {
        /// <summary>
        /// 当前使用的 Cookie
        /// </summary>
        public static CookieContainer Cookie { get; set; }

        /// <summary>
        /// 当前使用的编码
        /// </summary>
        public Encoding Encod { get; set; }

        /// <summary>
        /// Cookie 创建器
        /// </summary>
        public WebClientExpert Creator()
        {
            if (Cookie == null)
                Cookie = new CookieContainer();

            if(Encod == null)
                Encod = Encoding.UTF8;

            var webClient = new WebClientExpert(Cookie) {Encoding = Encod};

            return webClient;
        }

        /// <summary>
        /// 执行 Post 操作
        /// </summary>
        /// <param name="uri">请求地址</param>
        /// <param name="body">提交的数据</param>
        /// <returns></returns>
        public ResponseResult Post(Uri uri, NameValueCollection body)
        {
            using (var webClient = Creator())
            {
                var result = new ResponseResult();
                try
                {
                    var bytes = webClient.UploadValues(uri, body);
                    result.Success = true;
                    result.Result = webClient.Encoding.GetString(bytes);
                }
                catch (WebException e)
                {
                    result.Success = false;
                    var stream = e.Response.GetResponseStream();
                    if (stream != null)
                    {
                        var length = stream.Length;
                        var bytes = new byte[length];
                        stream.Read(bytes, 0, (int)length);
                        result.Result = webClient.Encoding.GetString(bytes);
                    }
                    result.Exception = e;
                }
                return result;
            }
        }

        /// <summary>
        /// 执行 Post 操作
        /// </summary>
        /// <param name="uri">请求地址</param>
        /// <param name="body">提交的数据(Json 格式)</param>
        /// <returns></returns>
        public ResponseResult Post(Uri uri, string body)
        {
            using (var webClient = Creator())
            {
                webClient.Headers.Set(HttpRequestHeader.ContentType, "application/json");
                var result = new ResponseResult();
                try
                {
                    var resultString = webClient.UploadString(uri, body);
                    result.Success = true;
                    result.Result = resultString;
                }
                catch (WebException e)
                {
                    result.Success = false;
                    var stream = e.Response.GetResponseStream();
                    if (stream != null)
                    {
                        var length = stream.Length;
                        var bytes = new byte[length];
                        stream.Read(bytes, 0, (int)length);
                        result.Result = webClient.Encoding.GetString(bytes);
                    }
                    result.Exception = e;
                }
                return result;
            }
        }
    }
}
